﻿using CPPEI.ETL.TaskAgent.Threads;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.TaskAgent
{
    /// <summary>
    /// 此程序负责处理任务的运行
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            CPPEI.Logger.InitByLogDirectory(System.Configuration.ConfigurationManager.AppSettings["LogDirectory"]);
            CPPEI.ETL.Domain.TaskProxy.TaskProxyManager.Init4Monitor(System.Configuration.ConfigurationManager.AppSettings["TaskDirectory"]);
            CPPEI.ETL.TaskAgent.Cache.WebBaseAddress = System.Configuration.ConfigurationManager.AppSettings["WebBaseAddress"];

            if (args != null && args.Length > 0)
            {
                try
                {
                    for (int index = 0; index < args.Length; index += 2)
                    {
                        if (string.Equals(args[index], "/key", StringComparison.CurrentCultureIgnoreCase))
                        {
                            Cache.Key = new Guid(args[index + 1]);
                            Logger.InfoToTag(Library.LogTAG, string.Format("程序启动,key:{0}", Cache.Key));
                        }
                    }
                }
                catch(Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTAG, string.Format("启动程序失败,ex:{0}", ex));
                    return;
                }
                
                Logger.InfoToTag(Library.LogTAG, string.Format("启动程序成功, ver:{0}, Key:{1}",  typeof(Program).Assembly.GetName().Version, Cache.Key));

                ScanThread.Start();
                RunThread.Start();

                string cmd = Console.ReadLine();
                while (cmd != "exit")
                {
                    System.Threading.Thread.Sleep(100);
                    cmd = Console.ReadLine();
                }

                RunThread.Stop();
                ScanThread.Stop();

                Logger.InfoToTag(Library.LogTAG, "启动程序完成");
            }
        }
    }
}
